package org.maxgroundstation.groundstation.view.overlay;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.video.VideoService;
import org.maxgroundstation.groundstation.view.VideoDisplayUtil;

/**
 * An overlay graphic for video image data.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class VideoImageDataOverlay implements Overlay {

    // Data members.
    private GroundStation gs;
    private boolean display;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public VideoImageDataOverlay(GroundStation gs) {
        // Initialize data members.
        this.gs = gs;
        display = true;
    }
    
    @Override
    public boolean canDisplay() {
        return (getVideoServices().size() > 0);
    }

    @Override
    public boolean getDisplay() {
        return display;
    }

    @Override
    public String getName() {
        return "Video Image Data";
    }

    @Override
    public void paintOverlay(Graphics g, int panelWidth, int panelHeight) {
        Graphics2D g2d = (Graphics2D) g;
        
        g2d.setColor(Color.GREEN);
        g2d.setFont(new Font(g2d.getFont().getFontName(), Font.BOLD, 10));
        
        // Get current video service being displayed.
        String videoServiceName = VideoDisplayUtil.getCurrentVideoServiceName();
        VideoService currentService = null;
        Iterator<VideoService> i = getVideoServices().iterator();
        while (i.hasNext()) {
            VideoService service = i.next();
            if (service.getName().equals(videoServiceName)) {
                currentService = service;
            }
        }
        
        if (currentService != null) {
            // Display video image width, height and FPS.
            String videoDimensionString = videoServiceName + ": " + getVideoWidth() + 
                    " x " + getVideoHeight() + " at " + (int) currentService.getVideoRate() + " FPS";
            TextDisplayUtil.drawText(videoDimensionString, (panelWidth / 2), panelHeight - 50, g);
        }
    }

    @Override
    public void setDisplay(boolean displayOverlay) {
        this.display = displayOverlay;
    }
    
    /**
     * Gets all video services.
     * @return list of video services.
     */
    private List<VideoService> getVideoServices() {
        List<VideoService> result = new ArrayList<VideoService>();
        
        Iterator<Service> i = gs.getServiceComm().getServices().iterator();
        while (i.hasNext()) {
            Service service = i.next();
            
            // Add any services with "video" in their name.
            if (service.getName().toLowerCase().indexOf("video") >= 0) {
                result.add((VideoService) service);
            }
        }
        
        return result;
    }
    
    /**
     * Gets the current video width.
     * @return video width.
     */
    private int getVideoWidth() {
        String videoImageName = VideoDisplayUtil.getCurrentVideoServiceName();
        ImageIcon videoImage = VideoDisplayUtil.getCurrentVideoImage(videoImageName);
        if (videoImage != null) return videoImage.getIconWidth();
        else return 0;
    }
    
    /**
     * Gets the current video height.
     * @return video height.
     */
    private int getVideoHeight() {
        String videoImageName = VideoDisplayUtil.getCurrentVideoServiceName();
        ImageIcon videoImage = VideoDisplayUtil.getCurrentVideoImage(videoImageName);
        if (videoImage != null) return videoImage.getIconHeight();
        else return 0;
    }
}